#!/bin/bash
HOME="/home"
MYSQL_FILES="mysql-5.6.51.tar.gz"
MYSQL_FILES_DIR="mysql-5.6.51"
#MYSQL_URL="http://mirrors.sohu.com/mysql/MySQL-5.6/$MYSQL_FILES"
MYSQL_URL="https://mirrors.aliyun.com/mysql/MySQL-5.6/$MYSQL_FILES"
MYSQL_PREFIX="/usr/local/mysql56"
#创建用户及用户组方法
function createUserGroupAndUser() {
    group=$1
    user=$2
    egrep "^$group" /etc/group >&/dev/null
    #创建用户组
    if [[ $? -ne 0 ]]; then
        groupadd $group
    fi
    #创建用户组下面的用户
    egrep "^$user" /etc/passwd >&/dev/null
    if [ $? -ne 0 ]; then
        useradd -r -s /sbin/nologin -g $group $user
    fi
}

function installMysql() {
    rpm -q tar &>/dev/null
    if [ $? -ne 0 ]; then
        yum install tar -y
    fi
    rpm -q gcc &>/dev/null
    if [ $? -ne 0 ]; then
        yum install gcc -y
    fi
    rpm -q gcc-c++ &>/dev/null
    if [ $? -ne 0 ]; then
        yum install gcc-c++ -y
    fi
    rpm -q openssl &>/dev/null
    if [ $? -ne 0 ]; then
        yum install openssl -y
    fi
    rpm -q openssl-devel &>/dev/null
    if [ $? -ne 0 ]; then
        yum install openssl-devel -y
    fi
    rpm -q autoconf &>/dev/null
    if [ $? -ne 0 ]; then
        yum install autoconf -y
    fi
    rpm -q wget &>/dev/null
    if [ $? -ne 0 ]; then
        yum install wget -y
    fi
    rpm -q cmake &>/dev/null
    if [ $? -ne 0 ]; then
        yum install cmake -y
    fi
    rpm -q ncurses &>/dev/null
    if [ $? -ne 0 ]; then
        yum install ncurses -y
    fi
    rpm -q ncurses-devel &>/dev/null
    if [ $? -ne 0 ]; then
        yum install ncurses-devel -y
    fi
    rpm -q ncurses-libs &>/dev/null
    if [ $? -ne 0 ]; then
        yum install ncurses-libs -y
    fi
    createUserGroupAndUser mysql mysql
    if [ ! -d "$HOME" ]; then
        mkdir $HOME
    fi
    cd "$HOME"
    if [ ! -f "$HOME/$MYSQL_FILES" ]; then
        wget -c $MYSQL_URL && tar -xzvf $MYSQL_FILES
    fi
    if [ ! -d "$HOME/$MYSQL_FILES_DIR" ]; then
        tar -xzvf $MYSQL_FILES
    fi
    cd $MYSQL_FILES_DIR
    cmake . -DCMAKE_INSTALL_PREFIX=$MYSQL_PREFIX \
    -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
    -DMYSQL_DATADIR=/data/mysql56 \
    -DSYSCONFDIR=/etc \
    -DMYSQL_USER=mysql \
    -DMYSQL_TCP_PORT=3306 \
    -DWITH_XTRADB_STORAGE_ENGINE=1 \
    -DWITH_INNOBASE_STORAGE_ENGINE=1 \
    -DWITH_PARTITION_STORAGE_ENGINE=1 \
    -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
    -DWITH_MYISAM_STORAGE_ENGINE=1 \
    -DWITH_READLINE=1 \
    -DENABLED_LOCAL_INFILE=1 \
    -DWITH_EXTRA_CHARSETS=1 \
    -DDEFAULT_CHARSET=utf8 \
    -DDEFAULT_COLLATION=utf8_general_ci \
    -DEXTRA_CHARSETS=all \
    -DWITH_BIG_TABLES=1 \
    -DWITH_DEBUG=0
    make && make install
    if [ $? -eq 0 ]; then
        #msyql的配置文件的移动过
        /bin/cp support-files/my-default.cnf /etc/my.cnf
        sed -i "/basedir/d" $MYSQL_PREFIX/my.cnf
        sed -i "/datadir/d" $MYSQL_PREFIX/my.cnf
        sed -i "/\[mysqld\]/abasedir=$MYSQL_PREFIX" $MYSQL_PREFIX/my.cnf
        sed -i "/\[mysqld\]/adatadir=$MYSQL_PREFIX/data/mysql56" $MYSQL_PREFIX/my.cnf
        #msyql的开机启动
        /bin/cp support-files/mysql.server /etc/init.d/mysqld
        chmod +x /etc/init.d/mysqld
        chkconfig --add /etc/init.d/mysqld
        chkconfig mysqld on
        #初始化数据库
        cd $MYSQL_PREFIX && mkdir -p ${MYSQL_PREFIX}/data/mysql56 && chown -R mysql:mysql ./ && ./scripts/mysql_install_db --user=mysql --datadir=/usr/local/mysql56/data/mysql56
        #软链接
        ln -s $MYSQL_PREFIX/bin/mysql /usr/bin/mysql
        echo -e "\n\033[32m———————————————–\033[0m"
        echo -e "\033[32mServer Install Success !\033[0m"
        echo -e "\033[32mvim /etc/init.d/mysqld\033[0m"
        echo -e "\033[32mbasedir=$MYSQL_PREFIX\033[0m"
        echo -e "\033[32mdatadir=$MYSQL_PREFIX/data/mysql56\033[0m"
        echo -e "\033[32mvim /usr/local/mysql56/my.cnf\033[0m"
        echo -e "\033[32mbasedir=$MYSQL_PREFIX\033[0m"
        echo -e "\033[32mdatadir=$MYSQL_PREFIX/data/mysql56\033[0m"
        echo -e "\033[32mservice mysqld stop|restart|start|status\033[0m"
    else
        echo -e "\033[32mMake or Make install ERROR,(rm CMakeCache.txt解决安装依赖重新安装)\033[0m"
        exit 0
    fi
}

installMysql
